#Region "Using Directives"

Imports System

#End Region

Namespace C4F.DevKit.Bluetooth.ObjectExchange

    ''' <summary>
    ''' Contains functions for formatting values as required during 
    ''' ObjectExchange.
    ''' </summary>
    Public NotInheritable Class ObjectExchangeHelper

        Private Sub New()
        End Sub

#Region "Public Static Functions"

        ''' <summary>
        ''' If the ushort value passed has Little Endian encoding, the function
        ''' returns a ushort value with Big Endian encoding.
        ''' Similarly, if the ushort value passed has Big Endian encoding, the
        ''' function returns a ushort value with Little Endian encoding.
        ''' </summary>
        ''' <param name="value">Ushort value to be operated on.</param>
        ''' <returns>Formatted ushort value.</returns>
        Public Shared Function FormatUInt16(ByVal value As UShort) As UShort

            If BitConverter.IsLittleEndian Then
                Return Convert.ToUInt16(CInt(((value >> 8) Or ((value << 8) And 65535))))
            Else
                Return value
            End If

        End Function

        ''' <summary>
        ''' If the uint value passed has Little Endian encoding, the function
        ''' returns a uint value with Big Endian encoding.
        ''' Similarly, if the uint value passed has Big Endian encoding, the
        ''' function returns a uint value with Little Endian encoding.
        ''' </summary>
        ''' <param name="value">Uint value to be operated on.</param>
        ''' <returns>Formatted uint value.</returns>
        Public Shared Function FormatUInt32(ByVal value As UInteger) As UInteger

            If BitConverter.IsLittleEndian Then
                Return ((((value << 24) And 4278190080) Or ((value << 8) And 16711680)) Or ((value >> 8) And 65280)) Or ((value >> 24) And 255)

            Else
                Return value
            End If

        End Function

        ''' <summary>
        ''' Throws an ObjectExchangeException depending on the specified opcode.
        ''' </summary>
        ''' <param name="opcode">An ObjectExchange opcode based on which the 
        ''' exception has to be generated and thrown.</param>
        ''' <exception cref="ObjectExchangeException">An ObjectExchangeException
        ''' with the ErrorType set appropriately depending on the specified 
        ''' opcode is raised.</exception>
        Public Shared Sub RaiseObjectExchangeError(ByVal opcode As ObjectExchangeOpcode)
            ' Take action based on the response sent by the Object Exchange 
            ' Service.
            Select Case opcode

                Case ObjectExchangeOpcode.Forbidden, ObjectExchangeOpcode.ForbiddenFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.Forbidden, ObjectExchangeResources.Error_Forbidden)

                Case ObjectExchangeOpcode.NotFound, ObjectExchangeOpcode.NotFoundFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.NotFound, ObjectExchangeResources.Error_NotFound)

                Case ObjectExchangeOpcode.Conflict, ObjectExchangeOpcode.ConflictFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.Conflict, ObjectExchangeResources.Error_Conflict)

                Case ObjectExchangeOpcode.Unauthorized, ObjectExchangeOpcode.UnauthorizedFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.SecurityError, ObjectExchangeResources.Error_Unauthorized)

                Case ObjectExchangeOpcode.NotAcceptable, ObjectExchangeOpcode.NotAcceptableFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.NotAcceptable, ObjectExchangeResources.Error_NotAcceptable)

                Case ObjectExchangeOpcode.InternalServerError, ObjectExchangeOpcode.InternalServerErrorFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.InternalServerError, ObjectExchangeResources.Error_InternalServerError)

                Case ObjectExchangeOpcode.ServiceUnavailable, ObjectExchangeOpcode.ServiceUnavailableFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.ServiceUnavailable, ObjectExchangeResources.Error_ServiceUnavailable)

                Case ObjectExchangeOpcode.BadRequest, ObjectExchangeOpcode.BadRequestFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.BadRequest, ObjectExchangeResources.Error_BadRequest)

                Case ObjectExchangeOpcode.MethodNotAllowed, ObjectExchangeOpcode.MethodNotAllowedFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.MethodNotAllowed, ObjectExchangeResources.Error_MethodNotAllowed)

                Case ObjectExchangeOpcode.NotModified, ObjectExchangeOpcode.NotModifiedFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.NotModified, ObjectExchangeResources.Error_NotModified)

                Case ObjectExchangeOpcode.NotImplemented, ObjectExchangeOpcode.NotImplementedFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.NotImplemented, ObjectExchangeResources.Error_NotImplemented)

                Case ObjectExchangeOpcode.RequestTimeOut, ObjectExchangeOpcode.RequestTimeOutFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.Timeout, ObjectExchangeResources.Error_RequestTimeOut)

                Case ObjectExchangeOpcode.MovedPermanently, ObjectExchangeOpcode.MovedPermanentlyFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.ResourceMoved, ObjectExchangeResources.Error_MovedPermanently)

                Case ObjectExchangeOpcode.MovedTemporarily, ObjectExchangeOpcode.MovedTemporarilyFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.ResourceMoved, ObjectExchangeResources.Error_MovedTemporarily)

                Case ObjectExchangeOpcode.SeeOther, ObjectExchangeOpcode.SeeOtherFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.ResourceMoved, ObjectExchangeResources.Error_SeeOther)

                Case ObjectExchangeOpcode.UseProxy, ObjectExchangeOpcode.UseProxyFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.SecurityError, ObjectExchangeResources.Error_UseProxy)

                Case ObjectExchangeOpcode.ProxyAuthenticationRequired, ObjectExchangeOpcode.ProxyAuthenticationRequiredFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.SecurityError, ObjectExchangeResources.Error_ProxyAuthRequired)

                Case ObjectExchangeOpcode.Gone, ObjectExchangeOpcode.GoneFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.NotFound, ObjectExchangeResources.Error_Gone)

                Case ObjectExchangeOpcode.LengthRequired, ObjectExchangeOpcode.LengthRequiredFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.AdditionalInfoRequired, ObjectExchangeResources.Error_LengthRequired)

                Case ObjectExchangeOpcode.PreconditionFailed, ObjectExchangeOpcode.PreconditionFailedFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.AdditionalInfoRequired, ObjectExchangeResources.Error_PreconditionFailed)

                Case ObjectExchangeOpcode.UnsupportedMediaType, ObjectExchangeOpcode.UnsupportedMediaTypeFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.UnsupportedMediaType, ObjectExchangeResources.Error_UnsupportedMediaType)

                Case ObjectExchangeOpcode.RequestedEntityTooLarge, ObjectExchangeOpcode.RequestedEntityTooLargeFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.RequestedEntityTooLarge, ObjectExchangeResources.Error_RequestedEntityTooLarge)

                Case ObjectExchangeOpcode.RequestUrlTooLarge, ObjectExchangeOpcode.RequestUrlTooLargeFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.RequestUrlTooLarge, ObjectExchangeResources.Error_RequestedUrlTooLarge)

                Case ObjectExchangeOpcode.BadGateway, ObjectExchangeOpcode.BadGatewayFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.GatewayError, ObjectExchangeResources.Error_BadGateway)

                Case ObjectExchangeOpcode.GatewayTimeout, ObjectExchangeOpcode.GatewayTimeoutFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.GatewayError, ObjectExchangeResources.Error_GatewayTimeout)

                Case ObjectExchangeOpcode.HttpVersionNotSupported, ObjectExchangeOpcode.HttpVersionNotSupportedFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.NotAcceptable, ObjectExchangeResources.Error_HttpVersionNotSupported)

                Case ObjectExchangeOpcode.DatabaseFull, ObjectExchangeOpcode.DatabaseFullFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.DatabaseError, ObjectExchangeResources.Error_DatabaseFull)

                Case ObjectExchangeOpcode.DatabaseLocked, ObjectExchangeOpcode.DatabaseLockedFinal
                    Throw New ObjectExchangeException(ObjectExchangeErrorType.DatabaseError, ObjectExchangeResources.Error_DatabaseLocked)

            End Select

        End Sub

#End Region

    End Class

End Namespace

